import random
import time
import string
from random import choice
from django.contrib.auth.models import User
from appinput.models import App
from envx.models import Env
from deploy.models import DeployPool, DeployStatus


def makename(date_no):
    # 随机生成两个字母
    random_letter = ''.join(random.sample(string.ascii_letters, 2))
    # 将当前时间以指定格式输出为字符串
    time_str = time.strftime("%Y-%m-%d-%H%M%S", time.localtime())
    # 将字符串以“-”拆分，装进数组中
    fake_time_str = time_str.split("-")
    # 将数组中的第3个元素（日期）替换为传入的数字
    fake_time_str[2] = str(date_no)
    # 将数组重新用“-”链接起来
    fake_time_str = '-'.join(fake_time_str)
    # 将新字符串和随机生成的字母（编程大写）拼接起来
    name = fake_time_str + random_letter.upper()
    return name


def fake_deploy_data():
    DeployPool.objects.all().delete()
    print('delete all deploy data')

    app_set = App.objects.all()
    env_set = Env.objects.all()
    user_set = User.objects.all()
    is_inc_tot = ['TOT', 'INC']
    deploy_type = ['deployall', 'deploypkg', 'deploycfg']
    deploy_status_set_env = DeployStatus.objects.filter(name__in=['READY', 'ING', 'FINISH', 'ERROR'])
    deploy_status_set_create = DeployStatus.objects.get(name='CREATE')
    deploy_status_set_build = DeployStatus.objects.get(name='BUILD')

    for date_no in range(30):
        name = makename(date_no=date_no)
        DeployPool.objects.create(name=name, description="test", branch_build="master", jenkins_number=date_no,
                                  code_number=date_no + 10, is_inc_tot=choice(is_inc_tot),
                                  deploy_type=choice(deploy_type),
                                  create_user=choice(user_set), app_name=choice(app_set), env_name=choice(env_set),
                                  # 已经流转的发布单，nginx有数，deploy_status为REDAY,ING,FINISH,ERROR中的一种
                                  deploy_status=choice(deploy_status_set_env), nginx_url="http://192.168.58.130/")

    for date_no in range(30):
        name = makename(date_no=date_no)
        if date_no % 2 == 1:
            DeployPool.objects.create(name=name,
                                      description="test",
                                      branch_build="master",
                                      jenkins_number=date_no,
                                      code_number=date_no + 10,
                                      is_inc_tot=choice(is_inc_tot),
                                      deploy_type=choice(deploy_type),
                                      create_user=choice(user_set),
                                      app_name=choice(app_set),
                                      # 刚新建的发布单，nginx_url没有数，deploy_status只能是’CREATE‘
                                      deploy_status=deploy_status_set_create,
                                      )
        else:
            DeployPool.objects.create(name=name,
                                      description="test",
                                      branch_build="master",
                                      jenkins_number=date_no,
                                      code_number=date_no + 10,
                                      is_inc_tot=choice(is_inc_tot),
                                      deploy_type=choice(deploy_type),
                                      create_user=choice(user_set),
                                      app_name=choice(app_set),
                                      # 编译通过的发布单，nginx_url有数，deploy_status只能是“BUILD”
                                      deploy_status=deploy_status_set_build,
                                      nginx_url="http://192.168.58.130/"
                                      )
    print('create all deploy data')
